import { axconst } from '@/shared/axconst';
import { notification } from 'antd';

export const databasePrefix = 'http://localhost:10001';
const pagination = axconst.page.params;
export const axhttp = {
  /** 重写登录 */
  login() {
    window.location.href = '/#/login';
  },

  checkIsLogin() {
    const loginFlag = localStorage.getItem('is_login');
    if (!(loginFlag && loginFlag === 'true')) {
      axhttp.login();
    }
  },

  async postFormXhr(url, data) {
    axhttp.checkIsLogin();

    return new Promise((resolve, reject) => {
      let dataArr = [];
      for (let k in data) {
        dataArr.push(k + '=' + data[k]);
      }

      const xhr = new XMLHttpRequest();
      xhr.open('post', databasePrefix + url, true);
      xhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded; charset=UTF-8');
      // xhr.withCredentials = true;
      xhr.send(dataArr.join('&'));
      xhr.onreadystatechange = () => {
        if (xhr.readyState == 4) {
          if (xhr.status == 200) {
            var res = JSON.parse(xhr.responseText);
            if (res.success === true) {
              return resolve(res);
            } else {
              notification.error({
                message: res.message,
              });
            }
          }
        }
      };
    });
  },

  async getListXhr(url, data) {
    axhttp.checkIsLogin();

    let dataArr = [];
    const params = {
      ...pagination,
      ...data,
    };

    for (let k in params) {
      dataArr.push(k + '=' + params[k]);
    }

    return new Promise((resolve, reject) => {
      const xhr = new XMLHttpRequest();
      // xhr.withCredentials = true;
      xhr.open('get', databasePrefix + url + '?' + dataArr.join('&'), true);
      xhr.send();
      xhr.onreadystatechange = () => {
        if (xhr.readyState == 4) {
          this.progress = 0;

          if (xhr.status == 200) {
            var res = JSON.parse(xhr.responseText);
            if (res.success === true) {
              return resolve({
                ...res,
                pagination: {
                  current: params.currentPage,
                  pageSize: params.pageSize,
                  total: res.total,
                },
              });
            } else {
              notification.error({
                message: res.message,
              });
            }
          }
        }
      };
    });
  },

  async getXhr(url, data = {}) {
    let dataArr = [];
    const params = {
      ...data,
    };
    for (let k in params) {
      dataArr.push(k + '=' + params[k]);
    }

    return new Promise((resolve, reject) => {
      const xhr = new XMLHttpRequest();
      // xhr.withCredentials = true;
      xhr.open('get', databasePrefix + url + '?' + dataArr.join('&'), true);
      xhr.send();
      xhr.onreadystatechange = () => {
        if (xhr.readyState == 4) {
          if (xhr.status == 200) {
            var res = JSON.parse(xhr.responseText);
            if (res.success === true) {
              return resolve(res);
            } else {
              notification.error({
                message: res.message,
              });
            }
          }
        }
      };
    });
  },

  async loginXhr(url, data) {
    let dataArr = [];
    const params = {
      ...pagination,
      ...data,
    };
    for (let k in params) {
      dataArr.push(k + '=' + params[k]);
    }

    return new Promise((resolve, reject) => {
      const xhr = new XMLHttpRequest();
      // xhr.withCredentials = true;
      xhr.open('get', databasePrefix + url + '?' + dataArr.join('&'), true);
      xhr.send();
      xhr.onreadystatechange = () => {
        if (xhr.readyState == 4) {
          this.progress = 0;

          if (xhr.status == 200) {
            var res = JSON.parse(xhr.responseText);
            if (res.success === true) {
              return resolve({
                ...res,
                pagination: {
                  current: params.currentPage,
                  pageSize: params.pageSize,
                  total: res.total,
                },
              });
            } else {
              notification.error({
                message: res.message,
              });
            }
          }
        }
      };
    });
  },

  async registerXhr(url, data) {
    return new Promise((resolve, reject) => {
      let dataArr = [];
      for (let k in data) {
        dataArr.push(k + '=' + data[k]);
      }

      const xhr = new XMLHttpRequest();
      xhr.open('post', databasePrefix + url, true);
      xhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded; charset=UTF-8');
      // xhr.withCredentials = true;
      xhr.send(dataArr.join('&'));
      xhr.onreadystatechange = () => {
        if (xhr.readyState == 4) {
          if (xhr.status == 200) {
            var res = JSON.parse(xhr.responseText);
            if (res.success === true) {
              return resolve(res);
            } else {
              notification.error({
                message: res.message,
              });
            }
          }
        }
      };
    });
  },
};
